//
//  LocalFileListViewController.m
//  SDKDemo
//
//  Created by 彭月辉 on 2022/6/28.
//

#import "LocalFileListViewController.h"
#import <AVFoundation/AVFoundation.h>

#import "PlaybackViewController.h"

@interface LocalFileListViewController (){
    NSArray *array;
}

@end

@implementation LocalFileListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"文件列表";
    [self readLocalFile];
}

- (void)readLocalFile{
    array = [[NSFileManager defaultManager] subpathsAtPath:[self getLocalDirectory:self.type]];
    [self.tableView reloadData];
}

- (NSString *)getLocalDirectory:(LinkDirType)type{
    NSString *main = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject;
    NSString *path;
    if(self.type == LINK_DIR_PHOTO){
        path = [main stringByAppendingPathComponent:@"photo"];
    }
    else if(self.type == LINK_DIR_VIDEO){
        path = [main stringByAppendingPathComponent:@"video"];
    }
    else if(self.type == LINK_DIR_EVENT){
        path = [main stringByAppendingPathComponent:@"video_emergency"];
    }
    return path;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString * path = [[self getLocalDirectory:self.type] stringByAppendingPathComponent:array[indexPath.row]];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellmark"];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellmark"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;;
    }
    cell.textLabel.text = array[indexPath.row];
//    cell.imageView.image = [UIImage imageNamed:@"DefaultImage"];
    if(self.type == LINK_DIR_PHOTO){
        cell.imageView.image = [UIImage imageWithContentsOfFile:path];
    }
    else{
        cell.imageView.image = [self assetGetThumImage:path];
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PlaybackViewController *playbackVC = [[PlaybackViewController alloc] init];
    playbackVC.localURL = [[self getLocalDirectory:self.type] stringByAppendingPathComponent:array[indexPath.row]];
    playbackVC.localType = self.type;
    [self.navigationController pushViewController:playbackVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

//滑动cell删除文件
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (editingStyle == UITableViewCellEditingStyleDelete)
        {
            // 删除数据
            NSString * path = [[self getLocalDirectory:self.type] stringByAppendingPathComponent:array[indexPath.row]];
            if([[NSFileManager defaultManager] isDeletableFileAtPath:path]){
                [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
                [self readLocalFile];
            }
        }
}

- (UIImage *)assetGetThumImage:(NSString *)path
{
    
    AVURLAsset *urlSet = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:path]];

    AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlSet];
    
    NSError *error = nil;
    CMTime time = CMTimeMake(0,10);//缩略图创建时间
    CMTime actucalTime; //缩略图实际生成的时间
    CGImageRef cgImage = [imageGenerator copyCGImageAtTime:time actualTime:&actucalTime error:&error];
    if (error) {
        NSLog(@"截取视频图片失败:%@",error.localizedDescription);
    }
    CMTimeShow(actucalTime);
    UIImage *image = [UIImage imageWithCGImage:cgImage];
//    UIImageWriteToSavedPhotosAlbum(image,nil, nil,nil);
    CGImageRelease(cgImage);
    return image;
    
}


@end
